package org.jeecg.modules.pubmgr.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.pubmgr.entity.SysProject;
import org.jeecg.modules.pubmgr.entity.SysProjectAuth;
import org.jeecg.modules.pubmgr.mapper.SysProjectAuthMapper;
import org.jeecg.modules.pubmgr.mapper.SysProjectMapper;
import org.jeecg.modules.pubmgr.service.ISysProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 应用工程表
 * @author： jeecg-boot
 * @date：   2019-05-17
 * @version： V1.0
 */
@Service
public class SysProjectServiceImpl extends ServiceImpl<SysProjectMapper, SysProject> implements ISysProjectService {

	@Autowired
	private SysProjectMapper sysProjectMapper;
	@Autowired
	private SysProjectAuthMapper sysProjectAuthMapper;
	
	@Override
	@Transactional
	public void saveMain(SysProject sysProject, List<SysProjectAuth> sysProjectAuthList) {
		sysProjectMapper.insert(sysProject);
		for(SysProjectAuth entity:sysProjectAuthList) {
			//外键设置
			entity.setFdProjid(sysProject.getFdProjid());
			sysProjectAuthMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(SysProject sysProject,List<SysProjectAuth> sysProjectAuthList) {
		sysProjectMapper.updateById(sysProject);
		
		//1.先删除子表数据
		sysProjectAuthMapper.deleteByMainId(sysProject.getFdProjid());
		
		//2.子表数据重新插入
		for(SysProjectAuth entity:sysProjectAuthList) {
			//外键设置
			entity.setFdProjid(sysProject.getFdProjid());
			sysProjectAuthMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		sysProjectMapper.deleteById(id);
		sysProjectAuthMapper.deleteByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			sysProjectMapper.deleteById(id);
			sysProjectAuthMapper.deleteByMainId(id.toString());
		}
	}
	
}
